package com.jt.www.biz.order_pc.controller;

import com.jt.www.biz.order_pc.service.InsuredInfoService;
import com.jt.www.domain.vo.InsuredUserInfoVo;
import com.jt.www.model.insured.InsuredUserInfoEntity;
import com.jt.www.model.reps.GenericDataResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @author admin_ltf
 * @create 2019-04-28 10:28
 */
@Api(value = "被保险人维护接口", tags = {"INSURED-INFO-API"}, description = "被保险人维护接口")
@RestController
@RequestMapping(value = "/insured")
public class InsuredInfoController {

    public static final Logger log = LoggerFactory.getLogger(InsuredInfoController.class);
    @Autowired
    private InsuredInfoService insuredInfoService;

    /**
     * 增加被保险人信息
     *
     * @param insuredUserInfo
     * @return
     */
    @ApiOperation(value = "增加被保险人信息", notes = "增加被保险人信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public ResponseEntity insertInsuredUserInfo(@RequestBody InsuredUserInfoEntity insuredUserInfo) {

        Integer num = insuredInfoService.addInsuredInfo(insuredUserInfo);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        } else if (num == 2) {
            //  // 被保险人名称不能重复校验代码，产品说 可以重复，所以注释掉
            // 2019 06 15  add by liangtf
            return GenericDataResponse.errorWithMsg("被保险人不能重复添加");
        }
        return GenericDataResponse.errorWithMsg("添加失败");
    }

    /**
     * 修改被保险人信息
     *
     * @param insuredUserInfo
     * @return
     */
    @ApiOperation(value = "修改被保险人信息", notes = "修改被保险人信息", httpMethod = "POST")
    @PostMapping(value = "/update")
    public ResponseEntity updateInsuredUserInfo(@RequestBody InsuredUserInfoEntity insuredUserInfo) {

        Integer num = insuredInfoService.updateInsuredInfo(insuredUserInfo);
        if (num == 1) {
            return GenericDataResponse.ok();
        } else if (num == -1) {
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        return GenericDataResponse.errorWithMsg("修改失败");
    }

    /**
     * 删除被保险人信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除被保险人信息", notes = "删除被保险人信息", httpMethod = "GET")
    @GetMapping(value = "/delete")
    public ResponseEntity deleteInsuredUserInfo(@ApiParam(value = "被保险人id", required = true) @RequestParam("id") String id) {

        if (StringUtils.isEmpty(id)) {
            log.error("【删除被保险人信息】id为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        Integer num = insuredInfoService.deleteInsuredInfo(id);
        if (num == 1) {
            return GenericDataResponse.ok();
        }
        return GenericDataResponse.errorWithMsg("删除失败");
    }

    /**
     * 根据usercode查询被保险人
     *
     * @param usercode
     * @return
     */
    @ApiOperation(value = "根据usercode查询被保险人", notes = "根据usercode查询被保险人", httpMethod = "GET")
    @GetMapping(value = "/getInfoByUsercode")
    public ResponseEntity searchInfoByUsercode(@ApiParam(value = "被保险人usercode", required = true) @RequestParam("usercode") String usercode) {

        if (StringUtils.isEmpty(usercode)) {
            log.error("【根据usercode查询被保险人】usercode为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }

//        List<InsuredUserInfoEntity> insuredUserInfoEntities = insuredInfoService.searchInfoByUserCode(usercode);
        return GenericDataResponse.okWithData(insuredInfoService.searchInfoByUserCode(usercode));
    }

    /**
     * 根据usercode查询被保险人
     * @param usercode
     * @param page
     * @param rows
     * @return
     */
//    @ApiOperation(value = "根据usercode查询被保险人",notes = "根据usercode查询被保险人",httpMethod = "GET")
//    @GetMapping(value = "/getInfoByUsercode")
//    public ResultDTO searchInfoByUsercode(@ApiParam(value = "被保险人usercode", required = true) @RequestParam("usercode") String usercode,
//                                          @ApiParam(value = "分页参数，第几页", required = true) @RequestParam(value="page",defaultValue = "1") Integer page,
//                                          @ApiParam(value = "分页参数，每页显示条数", required = true) @RequestParam(value = "rows",defaultValue = "10") Integer rows) {
//
//        if (StringUtils.isEmpty(usercode)) {
//            log.error("【根据usercode查询被保险人】usercode为空");
//            return new ResultDTO(ResultEnum.PARAM_ERROR, "参数不能为空");
//        }
//
//        HashMap<String, Object> resultMap = insuredInfoService.searchInfoByUserCode(usercode,page,rows);
//        return new ResultDTO(ResultEnum.SUCCESS, resultMap);
//    }


    /**
     * 根据id查询被保险人信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询被保险人信息", notes = "根据id查询被保险人信息", httpMethod = "GET")
    @GetMapping(value = "/getSingleInfoById")
    public ResponseEntity searchSingleInfo(@ApiParam(value = "被保险人id", required = true) @RequestParam("id") String id) {
        if (StringUtils.isEmpty(id)) {
            log.error("【根据id查询被保险人信息】id为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        InsuredUserInfoVo insuredUserInfoVo = insuredInfoService.searchSingleInfo(id);
        return GenericDataResponse.okWithData(insuredUserInfoVo);
    }

    /**
     * 判断被保险人信息是否需要签署协议
     *
     * @param userCode
     * @param insuredName
     * @return
     */
    @ApiOperation(value = "判断被保险人信息是否需要签署协议", notes = "判断被保险人信息是否需要签署协议:true(需要添加协议)，false(不需要添加协议)", httpMethod = "GET")
    @GetMapping(value = "/isSignAgreement")
    public ResponseEntity isSignAgreement(@ApiParam(value = "投保人code", required = true) @RequestParam(value = "userCode", required = true) String userCode,
                                          @ApiParam(value = "被保人名称", required = true) @RequestParam(value = "insuredName", required = true) String insuredName) {
        if (StringUtils.isEmpty(userCode)) {
            log.info("【判断被保险人信息是否需要签署协议】userCode为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        if (StringUtils.isEmpty(insuredName)) {
            log.info("【判断被保险人信息是否需要签署协议】insuredName为空");
            return GenericDataResponse.errorWithMsg("参数不能为空");
        }
        Boolean result = null;
        try {
            result = insuredInfoService.isSignAgreement(userCode, insuredName);
        } catch (Exception e) {
            e.printStackTrace();
            return GenericDataResponse.errorWithMsg("查询失败");
        }

//        GenericDataResponse.errorWithMsg("计算人数失败");
//    }
//        return GenericDataResponse.okWithData(num);
        return GenericDataResponse.okWithData(result);
    }


}
